<#
    .AUTHOR
        cw50406@imcnam.ssmb.com  
    .GROUP
        RPA AA STE Support Team(dl.gt.cn.cstc.rpa.aa.ste@imcnam.ssmb.com)
    .VERSION
        2.0
    .Synopsis
        Website Auto Login
    .DESCRIPTION
        This script is used to automatically login the website by given url and then check if it loads successfully.
    .EXAMPLE
        .\WebsiteAutoLogin.ps1 $cr_vault_status_url "vs" '"Status":"Opened"' 
        try to login vault status webpage and check if it loads successfully
    .EXAMPLE
        .\WebsiteAutoLogin.ps1 $cr_url "cr" 'Repository Manager' -sendEmail 
        try to login control room webpage and check if it loads successfully, and then send email to team with logfile and screenshot images.
#>
[CmdletBinding()]param(
    [Parameter(Mandatory=$true)]
    [string]$url,                           # the url that script will try to navigate
    [string]$url_type,                      # cr, cr-lb, vaultstatus ....
    [string]$expectedContent,               # string to check if url loads successfully
    [Parameter(Mandatory=$false)]
    [string]$web_login_user='nam\aaadmin',  # the user to be entered in the login popup window
    [string]$web_login_user_pwd,            # the password to be entered in the login popup window
    [string]$logfile,                       # the specified log file path 
    [AllowNull()]
    [int]$ie_position,                      # ie window position 1:left-top 2:right-top 3:left-bottom 4:right-bottom ==> for later screenshot capture use
    [switch]$FunctionInvoked,               # on ==> send email to user
    [switch]$sendEmail                      # on ==> send email to user
)
#<----------------------------------------------------------------------------------- initilize parames
$this_path = Split-Path -Parent ($MyInvocation.MyCommand.Definition)
If( !$FunctionInvoked ){. "$this_path\functions.ps1"}
$login_window_image="$this_path\$url_type-login-window.png"
$login_window_error_image="$this_path\$url_type-login-error.png"
$urlwebpage_success_image="$this_path\$url_type-webpage-success.png" 
$urlwebpage_error_image="$this_path\$url_type-webpage-error.png" 
If( !$PSBoundParameters.ContainsKey('web_login_user_pwd' ) ){$web_login_user_pwd=$this_user_pwd}
If( $PSBoundParameters.ContainsKey('logfile' ) ){$log_file=$logfile
}else{Write-Log Normal "----------------------------START OF SCRIPT----------------------------" $log_file}
Write-Log Normal "<<Start of $url_type website auto login>>" $log_file
Write-Log Normal "Trying to log in $url_type Website: $url" $log_file
$ie=Get-IEObject $ie_position; ps notepad -ea 0|kill -force -ea 0 ; $ie.HWND|Show-Window
$ie.Navigate($url)
If( Wait-LoginWindow $url_type $expectedContent $urlwebpage_success_image $urlwebpage_error_image $url $ie_position){
    Write-Log Normal "Trying to send user name and password" $log_file 
    Send-Creds $web_login_user $web_login_user_pwd $login_window_image $ie_position
    $with_title_ie_process=Get-NewIEProcess $old_ie_process|?{$_.MainWindowTitle -ne ""}
    If( $with_title_ie_process.count -gt 1 ){
        (gwmi win32_process|?{$_.name -imatch 'powershell'}).CommandLine|Out-File -FilePath $log_file -Append -Encoding utf8 -Force -ea 1
        Exit-Script 'Error' "Login window still exists after send creds, either $url_type website is unavailable or creds are invalid" $login_window_error_image
    }else{Wait-WebpageLoad $url_type $expectedContent $urlwebpage_success_image $urlwebpage_error_image $url $ie_position}
}
if($sendEmail){
    Write-Log Normal "----------------------------End OF SCRIPT----------------------------`n`n" $log_file
    $attached_files=@($log_file)
    If( ($images = (dir "$this_path\*.png").fullname) -ne $null ){
        If( $images -imatch "error" ){
            $result="ERROR" ; $full_screenshot_file="$this_path\CRErrorStatus.png"
        }else{
            $result="SUCCESS" ; $full_screenshot_file="$this_path\CRSuccessStatus.png"
        }
        Save-Screenshot $full_screenshot_file ; $attached_files+=$full_screenshot_file
    }
    Send-Mailtoteam $result $attached_files
    Export-LogToAutosys $autosys_out_log
}